package Controller;

import Service.PromotionSpaceService;
import domain.PromotionSpace;
import domain.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/PromotionSpace")
public class PromotionSpaceController {

    @Autowired
    private PromotionSpaceService promotionSpaceService;

    /*
         查询所有的广告信息
     */
    @RequestMapping("/findAllPromotionSpace")
    public ResponseResult findAllPromotionSpace(){

        List<PromotionSpace> promotionSpace = promotionSpaceService.findAllPromotionSpace();

        ResponseResult responseResult = new ResponseResult(true, 200, "查询所有广告位成功", promotionSpace);
        return responseResult;
    }


    /*
            添加 + 修改 广告信息
     */
    @RequestMapping("/saveOrUpdatePromotionSpace")
                                                    // 获取请求体中的内容 并封装到实体
    public ResponseResult saveOrUpdatePromotionSpace(@RequestBody PromotionSpace promotionSpace){
        ResponseResult result = null;
        if(promotionSpace.getId() == null) {

            // 新增操作
            promotionSpaceService.savePromotion(promotionSpace);
            result = new ResponseResult(true, 200, "新增广告位成功", null);

        }else{
            // 更新操作
            promotionSpaceService.updatePromotion(promotionSpace);
            result = new ResponseResult(true, 200, "更新广告位成功", null);
        }
        return result;
    }

    /*
         根据Id 查询广告位信息
    */
    @RequestMapping("/findPromotionSpaceById")
    public ResponseResult findPromotionSpaceById(int id){
        PromotionSpace promition = promotionSpaceService.findPromitionById(id);

        ResponseResult responseResult = new ResponseResult(true, 200, "信息回显成功", promition);
        return responseResult;


    }
}
